<?php
/**
 * CActionColumn class file.
 * 
 * @author Lucas Vasconcelos <contato@lucasvasconcelos.com>
 * @copyright Copyright(c) 2010 Lucas Vasconcelos
 * @license BSD License http://www.vasconcelossa.com/yap/license
 * @link http://www.vasconcelossa.com/yap/
 * @version $Id$
 * @package yap.web.ui.webControls
 */

/**
 * CActionColumn class.
 * 
 * Cria links de ação (Update, Delete) para cada registro. Deve ser usado como uma das colunas de
 * TDataGrid exigida por {@link CPageCrud}.
 * 
 * Os links podem ser do tipo THyperLink (default) ou TLinkButton.
 * 
 * @author Lucas Vasconcelos <contato@lucasvasconcelos.com>
 * @version 1.0 $Revision$
 * @package yap.web.ui.webControls
 */
class CActionColumn extends TDataGridColumn
{
	/**
	 * @param string valores aceitos: THyperLink e TLinkButton
	 */
	public function setLinkType($value)
	{
		if($value!=='THyperLink' && $value!=='TLinkButton')
			throw new TInvalidDataValueException('LinkType must be one of: THyperLink or TLinkButton');

		$this->setViewState('LinkType',$value,'THyperLink');
	}
	
	/**
	 * @return string THyperLink or TLinkButton
	 */
	public function getLinkType()
	{
		return $this->getViewState('LinkType','THyperLink');
	}
	
	/**
	 * @param string URL usada pelos links.
	 */
	public function setNavigateUrl($value)
	{
		$this->setViewState('NavigateUrl', $value, '');
	}

	/**
	 * @return string URL usada pelos links.
	 */
	public function getNavigateUrl()
	{
		return $this->getViewState('NavigateUrl', '');
	}

//	public function setUpdate($value)
//	{
//		$this->setViewState('Update',$value,CActionLinkStatus::Normal);
//	}
//
//	public function getUpdate()
//	{
//		return $this->getViewState('Update',CActionLinkStatus::Normal);
//	}
//
//	public function setDelete($value)
//	{
//		$this->setViewState('Delete',$value,CActionLinkStatus::Normal);
//	}
//	public function getDelete()
//	{
//		return $this->getViewState('Delete',CActionLinkStatus::Normal);
//	}

	/**
	 * Initializes the specified cell to its initial values.
	 * The default implementation sets the content of header and footer cells.
	 * If sorting is enabled by the grid and sort expression is specified in the column,
	 * the header cell will show a link/image button. Otherwise, the header/footer cell
	 * will only show static text/image.
	 * This method can be overriden to provide customized intialization to column cells.
	 * @param TTableCell the cell to be initialized.
	 * @param integer the index to the Columns property that the cell resides in.
	 * @param string the type of cell (Header,Footer,Item,AlternatingItem,EditItem,SelectedItem)
	 */
	public function initializeCell($cell,$columnIndex,$itemType)
	{
		if($this->Owner->NamingContainer instanceof CCrudPortlet)
			$allow=$this->Owner->NamingContainer->allowedActions();
		else
			$allow=$this->Owner->Page->allowedActions();
		
		if($itemType===TListItemType::Item || $itemType===TListItemType::AlternatingItem)
		{
			if(in_array(CActionType::Update,$allow))
			{
				$control=$this->createUpdateLink();
				//$control->setEnabled($this->getUpdate()!==CActionLinkStatus::Disabled);
				$cell->getControls()->add($control);
				$control->attachEventHandler('OnDataBinding',array($this,'dataBindColumn'));
			}

			if(in_array(CActionType::Delete,$allow))
			{
				$control=$this->createDeleteLink();
				//$control->setEnabled($this->getDelete()!==CActionLinkStatus::Disabled);
				$cell->getControls()->add($control);
				$control->attachEventHandler('OnDataBinding',array($this,'dataBindColumn'));
			}
		}
		else
			parent::initializeCell($cell,$columnIndex,$itemType);
	}

	/**
	 * Databinds a cell in the column.
	 * This method is invoked when datagrid performs databinding. 
	 */
	public function dataBindColumn($sender,$param)
	{
		$data=$sender->getNamingContainer()->getData();
		if($this->getLinkType()==='THyperLink')
			$sender->NavigateUrl.='&id='.$this->getDataFieldValue($data,'id');

		// Se houver uma coluna 'editavel' usa para determinar se o idem deve ser visivel.
		if(null!==$editable=$this->getDataFieldValue($data,'editavel'))
			$sender->Visible=TPropertyValue::ensureBoolean($editable);
		//else
		//	$sender->setCommandParameter($this->getDataFieldValue($data,'id'));
	}

	/**
	 * Inicializa coluna com o estilo CSS 'act-column'.
	 * @see Web/UI/WebControls/TDataGridColumn#initialize()
	 */
	public function initialize()
	{
		$this->getItemStyle()->setCssClass('act_column');
	}

	/**
	 * Inicializa um weblink do tipo definido, para executar a ação {@link CActionType::Update}.
	 * @return mixed THyperLink|TLinkButton
	 */
	private function createUpdateLink()
	{
		$control=Prado::createComponent('System.Web.UI.WebControls.'.$this->getLinkType());
		$control->setText('Editar');
		$control->setToolTip('Editar');
		if($this->getLinkType()==='THyperLink')
			$control->setNavigateUrl($this->getNavigateUrl().'&amp;a='.CActionType::Update);
		else
			$control->setCommandName(CActionType::Update);
		$control->setCssClass('edit');
		return $control;
	}

	/**
	 * Inicializa um weblink do tipo definido, para executar a ação {@link CActionType::Delete}.
	 * @return mixed THyperLink|TLinkButton
	 */
	private function createDeleteLink()
	{
		$control=Prado::createComponent('System.Web.UI.WebControls.'.$this->getLinkType());
		$control->setText('Excluir');
		$control->setToolTip('Excluir');
		if($this->getLinkType()==='THyperLink')
			$control->setNavigateUrl($this->getNavigateUrl().'&amp;a='.CActionType::Delete);
		else
			$control->setCommandName(CActionType::Delete);
		$control->getAttributes()->onclick='return confirm(\'Tem certeza que deseja excluir este registro?\');';
		$control->setCssClass('del');
		return $control;
	}
}

class CActionLinkStatus extends TEnumerable
{
	const Normal='Normal';
	const Disabled='Disabled';
	const Invisible='Invisible';
}